package com.mobiporter.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.mobiporter.adias.tags.model.MonthYear;

public class DateUtils 
{
	public static final Log log = LogFactory.getLog(DateUtils.class);
	public static final String[] months = new String[]{"JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"};
	private static final String DATE_PATTERN = "(\\d+)/(\\d+)/(\\d\\d\\d\\d)";
	private static final String INPUT_DATE_FORMAT = "dd/MM/yyyy";
	
	public static List<String> getDatesFromThisDate(Date d) 
	{
		Calendar cal = getCalendar() ;
		cal.setTime(d);
		int date = cal.get(Calendar.DAY_OF_MONTH);
		List<String> dates = new ArrayList<String>() ;
		int j = date ;
		while (isNotPrevousDay(j,date))
		{
			dates.add(j+"");
			if (j < 31 )
			{
				j++ ;
				continue ;
			}
			if (j==31)
			{
				j=1 ;
				continue ;
			}
		}
		dates.add(getPreviousDay(date)+"");
		return dates;
	}


	public static List<MonthYear> getMonthYearsFrom(Date d) 
	{
		List<MonthYear> monthYears = new ArrayList<MonthYear>() ;
		
		for (int i = 0;i<6 ;i++)
		{
			Calendar cal = getCalendar() ;
			cal.setTime(d);
			cal.add(Calendar.MONTH, i);
			int month = cal.get(Calendar.MONTH);
			int year = cal.get(Calendar.YEAR);
			MonthYear monthYear = new MonthYear(month,year);
			monthYears.add(monthYear);
		}
		return monthYears;
	}
	
	private static boolean isNotPrevousDay(int j, int date) 
	{
		if (j == getPreviousDay(date))
		{
			return false ;
		}
		else
			return true ;
	}
	
	public static int getPreviousDay(int date)
	{
		if (date == 1)
			return 31 ;
		else 
			return date-1 ;
	}


	public static Date getDateFromString(String dateString) 
	{
		if (dateString == null)
		{
			throw new IllegalArgumentException() ;
		}
		if (!isProperDateFormat(dateString,DATE_PATTERN))
		{
			throw new IllegalArgumentException() ;
		}
		if(!isValidDate(dateString,DATE_PATTERN))
		{
			throw new IllegalArgumentException() ;
		}
		try 
		{
			return new SimpleDateFormat(INPUT_DATE_FORMAT).parse(dateString);
		} 
		catch (ParseException e) 
		{
			return null ;
		}
	}
	
	/**
	 * Test 
	 * @param dateString
	 * @param datePattern
	 * @return
	 */
	public static boolean isValidDate(String dateString, String datePattern) 
	{
		Calendar cal = getCalendar() ;
		int year = getYearFromDateString(dateString, datePattern);
		cal.set(Calendar.YEAR, year);
		int month = getMonthFromDateString(dateString, datePattern);
		cal.set(Calendar.MONTH,month-1);
		int day = getDayFromDateString(dateString, datePattern);
		if (day > cal.getActualMaximum(Calendar.DAY_OF_MONTH) || day < 1)
			return false ;
		return true;
	}


	public static boolean isProperDateFormat(String dateString,String datePattern)
	{
		Pattern p = Pattern.compile(datePattern);
		Matcher matcher = p.matcher(dateString);
		return matcher.find() ;
	}


	public static int getMonthFromDateString(String dateString,
			String datePattern) 
	{
		String month = StringUtils.getGroup(2, datePattern, dateString);
		return Integer.parseInt(month);
	}


	public static int getYearFromDateString(String dateString,
			String datePattern) {
		String year = StringUtils.getGroup(3, datePattern, dateString);
		return Integer.parseInt(year);
	}


	public static int getDayFromDateString(String dateString, String datePattern) {
		String day = StringUtils.getGroup(1, datePattern, dateString);
		return Integer.parseInt(day);
	}


	public static String formatDate(Date time,String dateFormat) 
	{
		SimpleDateFormat f = new SimpleDateFormat(dateFormat);
		f.setTimeZone(TimeZone.getTimeZone("GMT+5:30"));
		return f.format(time);
	}
	
	public static String formatDate(Date time) 
	{
		SimpleDateFormat f = new SimpleDateFormat(INPUT_DATE_FORMAT);
		f.setTimeZone(TimeZone.getTimeZone("GMT+5:30"));
		return f.format(time);
	}


	public static Calendar getCalendar() {
		Calendar cal = Calendar.getInstance(getISTTimezone());
		return cal;
	}
	
	public static TimeZone getISTTimezone()
	{
		return TimeZone.getTimeZone("GMT+05:30");
	}


	public static int getDifferenceInMonths(Date fromDate, Date toDate)
	{
		Calendar cal = getCalendar() ;
		
		cal.setTime(fromDate);
		int baseYear = cal.get(Calendar.YEAR);
		int baseMonth = cal.get(Calendar.MONTH);
		
		cal.setTime(toDate);
		int targetYear = cal.get(Calendar.YEAR);
		int targetMonth = cal.get(Calendar.MONTH);
		
		int differenceYear = targetYear - baseYear ;
		int differenceMonth = targetMonth - baseMonth ;
		
		return differenceYear*12 + differenceMonth ;
	}


	public static int getDayFromDate(Date depDate)
	{
		Calendar cal = DateUtils.getCalendar() ;
		cal.setTime(depDate);
		return cal.get(Calendar.DAY_OF_MONTH);
	}


	public static int getMonthFromDate(Date depDate)
	{
		Calendar cal = DateUtils.getCalendar() ;
		cal.setTime(depDate);
		return cal.get(Calendar.MONTH) + 1;
	}


	public static List<String> getDates() 
	{
		ArrayList<String> dates = new ArrayList<String>();
		for (int i=1;i<32;i++)
		{
			if (i<10)
				dates.add("0"+i);
			else
				dates.add(i+"");
		}
		return dates;
	}
	
	public static String getMonFromDate(String departureDate) {
		String regex = "([\\d]+)/([\\d]+/[\\d]+)";
		Matcher matcher = Pattern.compile(regex).matcher(departureDate);
		if (matcher.find())
		{
			return matcher.group(2);
		}
		return null;
	}

	public static String getDayFromDate(String departureDate) {
		String regex = "([\\d]+)/([\\d]+/[\\d]+)";
		Matcher matcher = Pattern.compile(regex).matcher(departureDate);
		if (matcher.find())
		{
			return matcher.group(1);
		}
		return null;
	}
}
